package com.gitee;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author gcq
 * @Create 2022-07-05
 */
public class ThreadVs {


    /**
     * 将Word转换成PDF格式：处理时间很长的耗时过程
     */
    @Test
    public void oldHandle() throws InterruptedException {
        /**
         * 模拟多个用户请求
         */
        for(int i = 1; i <= 100; i++) {
            new Thread(() -> {
                System.out.println("文档处理开始");
                try {
                    Thread.sleep(1000L * 20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("文档处理结束");
            }).start();
        }
        Thread.sleep(1000L * 1000);
    }

    @Test
    public void newHandle() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for(int i = 1; i <= 100; i++) {
            executorService.execute(() -> {
                System.out.println("文档处理开始");
                try {
                    Thread.sleep(1000L * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("文档处理结束");
            });
        }
        Thread.sleep(1000L * 1000);
    }
}